package org.shiro.basic.exceptions.handler;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.shiro.basic.exceptions.SystemUserNotLoginException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Shiro异常处理器
 *
 * @author LiKun
 * @date 2021/11/3 22:27
 */
@RestControllerAdvice
public class ShiroExceptionHandler {
    public static final String PASSWORD_ERROR = "账号密码错误";
    public static final String ACCOUNT_IS_LOCKED = "该账号被锁定";
    public static final String ACCOUNT_IS_NOT_EXIST = "该账号不存在";
    public static final String SYSTEM_USER_NOT_LOGIN = "用户未登录，请登录后再试";
    private static final Logger logger = LoggerFactory.getLogger(ShiroExceptionHandler.class);

    @ExceptionHandler(LockedAccountException.class)
    public String lockAccountExceptionHandler() {
        return ACCOUNT_IS_LOCKED;
    }

    @ExceptionHandler(UnknownAccountException.class)
    public String unknownAccountExceptionHandler() {
        return ACCOUNT_IS_NOT_EXIST;
    }

    @ExceptionHandler(SystemUserNotLoginException.class)
    public String systemUserNotLoginExceptionHandler() {
        return SYSTEM_USER_NOT_LOGIN;
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    public String incorrectCredentialsExceptionHandler() {
        logger.info("账号密码错误...");
        return PASSWORD_ERROR;
    }
}
